/* eslint-disable eqeqeq *///忽略==的警告
import { mqtthHndleDate } from "./adminUtils"; /* eslint-disable eqeqeq */
import { isRGBDevice, isLightDevice } from './pidUtils'
import { homeDeng } from "../api/index"
import PubSub from 'pubsub-js'
import { meshLog, SPRemove, SPGet, SPSet } from "../config";
export let oldTime = 0; //时间戳
export let refreData = ''; //刷新数据
export let refreCount = 0; //刷新计数
export const delayTime = 5000; //延迟时间
export let mClicktime = 0; //时间戳
export const getMqttReceiveData = (panduan2, data) => {
    //2020 0e 62 0a00 0204 e6a507 1e 81 6666 ac0d 28 0202
    if (panduan2.length !== 4) {
        return;
    }
    oldTime = new Date().getTime();  //毫秒
    meshLog("mqttUtils.js,17行,wjk mqtt 0", oldTime, panduan2);
    let mqttbufa = (SPGet("mqttbufa"));
    if (mqttbufa == 0) {
        setTimeout(() => {
            PubSub.publish('mqttdanfa', '')
        }, 500)
    }
    //数据
    refreData = data + '&' + refreData;
    refreCount++;
    // meshLog("wjk mqtt count", refreCount);
    if (refreCount >= 10) {
        setRefreData();
    }
    //延迟delayTime后结束
    setTimeout(() => {
        setOverData();
    }, delayTime)
}
function setRefreData() {
    //刷新数据库
    const arr = refreData.split('&');   //字符串转数组
    refreData = '';
    refreCount = 0;
    //meshLog("wjk mqtt data", arr);
    for (let index = 0; index < arr.length - 1; index++) {
        let deviceSetx = JSON.parse(SPGet("deviceSetx"));
        let data1 = arr[index];
        let panduan = data1.substring(10, 12) + data1.substring(8, 10);//mqtt返回来灯的信息的中的唯一灯的标识
        let panduan2 = panduan.toUpperCase();//转换成大写的方法
        let dim = '';
        let tem = '';
        if (data1.substring(24, 26) == "82" || data1.substring(24, 26) == "02") {
            meshLog('mqttUtils.js,44行,灯是rgb的,mqtt回传的信息', data1);
            let onff = data1.substring(24, 26) == "82" ? '01' : '00'   // meshLog(onff);//灯的判断0还是1
            let isrgb = '01';
            let rgbL = (parseInt(data1.substring(28, 30) + data1.substring(26, 28), 16)) / 65535;
            let rgbH = (parseInt(data1.substring(32, 34) + data1.substring(30, 32), 16)) / 65535;
            let rgbS = (parseInt(data1.substring(36, 38) + data1.substring(34, 36), 16)) / 65535;
            tem = Math.ceil(rgbH * 6500)
            if (tem > 2200) {
                tem = tem
            } else {
                tem = 2300
            }
            // meshLog("rgbH", rgbH);
            // meshLog("rgbH", rgbS);
            // meshLog("rgbH", rgbL);
            //HSL 转 RGB
            let RGB = [123, 123, 123]
            //RGB转hsv
            let R = RGB[0]
            let G = RGB[1]
            let B = RGB[2]
            let HSV = [4561, 12345, 456]
            //RGB转hsv结束
            dim = Math.ceil((HSV[2]) * 65535)
            //查询属于这个单灯的 device
            let devSetItem = deviceSetx.filter((item) => {
                if (item.address == panduan2) {
                    item.dim = dim
                    item.tem = tem
                    item.isOnoff = onff
                    item.isrgb = isrgb
                    item.isOnline = isrgb
                    item.rgbH = rgbH
                    item.rgbS = rgbS
                    item.rgbL = rgbL
                    return item
                }
            })
            //meshLog('devSetItem', devSetItem);
            //修改数据库发一系列操作些列方法
            mqtthHndleDate(devSetItem)
        } else if (data1.substring(24, 26) == "81" || data1.substring(24, 26) == "01") {
            meshLog('mqttUtils.js,86行,灯是cct的状态,mqtt回传的信息', data1);
            let onff = data1.substring(24, 26) == "81" ? '01' : '00'   // meshLog(onff);//灯的判断0还是1
            dim = parseInt(parseInt((parseInt(data1.substring(28, 30) + data1.substring(26, 28), 16)) / 65535 * 100) / 100 * 65535);
            tem = (parseInt(data1.substring(32, 34) + data1.substring(30, 32), 16));
            let isOnline = '01';
            // meshLog('dim', dim);
            //查询属于这个单灯的 device
            let devSetItem = deviceSetx.filter((item) => {
                if (item.address == panduan2) {
                    item.dim = dim
                    item.tem = tem
                    item.isOnoff = onff
                    item.isOnline = isOnline
                    return item
                }
            })
            // meshLog('devSetItem', devSetItem);
            // meshLog('panduan2', panduan2);
            //在CCT状态下的灯也有可能是RGB的灯，所以要通过pid来判断
            let pid = devSetItem[0].pid
            //meshLog('pidpid', pid);
            let result = isRGBDevice(pid)
            if (result == true) {
                meshLog('mqttUtils.js,109行,是rgb的灯但不是rgb状态', result);
                devSetItem[0].isrgb = '01'
            } else {
                meshLog('mqttUtils.js,112行,resultresult1', result);
            }
            //修改数据库发一系列操作些列方法
            mqtthHndleDate(devSetItem)
        } else if (data1.substring(24, 26) == "80" || data1.substring(24, 26) == "00") {
            meshLog('mqttUtils.js,117行,灯是dim的,mqtt回传的信息', data1);
            let onff = data1.substring(24, 26) == "80" ? '01' : '00'   // meshLog(onff);//灯的判断0还是1
            dim = (parseInt(data1.substring(28, 30) + data1.substring(26, 28), 16));
            let isOnline = '01';
            //查询属于这个单灯的 device
            let devSetItem = deviceSetx.filter((item) => {
                if (item.address == panduan2) {
                    item.dim = dim
                    item.isOnoff = onff
                    item.isOnline = isOnline
                    item.isdimdeng = isOnline
                    return item
                }
            })
            //meshLog('devSetItem', devSetItem);
            //修改数据库发一系列操作些列方法
            mqtthHndleDate(devSetItem)
        } else {
            meshLog('mqttUtils.js,135行,不知道是什么灯');
        }
    }
    //刷新home界面
    let newTime = new Date().getTime();
    if (newTime - mClicktime < 1000) {
        return;
    }
    mClicktime = new Date().getTime();;
    PubSub.publish('homeshuaxin', '')
    PubSub.publish('refreshhome', '')//刷新home界面的所有数据
}
async function setOverData() {
    let newTime = new Date().getTime();
    if (newTime < (oldTime + delayTime)) {
        return;
    }
    //这边处理MQTT结束信息，mqtt返回有数据走这里，如果没有灯还是要处理一下
    setRefreData();
    PubSub.publish('setInterval', '')
    //除了补发要等一下时间（需要加只能补发一次）
    let mqttbufa = (SPGet("mqttbufa"));
    meshLog('mqtt.js. 162行 mqtt执行发送次数 第', mqttbufa);
    //meshLog('mqttbufa', mqttbufa);
    //setTimeout(async () => {
    if (mqttbufa == 0) {
        meshLog('mqttUtils.js,166行,第一次补发');//直接控制OK弹框
        let deviceSetx = JSON.parse(SPGet("deviceSetx"));
        let devisOnline = deviceSetx.filter((item) => {
            if ((item.isOnline == '01') && isLightDevice(item.pid)) {
                return item
            }
        })
        let deviceSet = deviceSetx.filter((item) => {
            if (isLightDevice(item.pid)) {
                return item
            }
        })
        let devisUnOnline = deviceSetx.filter((item) => {
            if ((item.isOnline == '00') && isLightDevice(item.pid)) {
                return item
            }
        })
        meshLog('mqttUtils.js, 183行,第一次补发的deviceSet.length', deviceSet.length);
        meshLog('mqttUtils.js, 183行,最第一次补发在线的deviceSet.length', devisOnline.length);
        if (devisUnOnline.length > 1) {
            const opCode = "E3A507";
            const token = SPGet('token')
            const email = SPGet('email')
            const meshUUID = SPGet('meshUUID')
            const values = "1E";
            let adds = devisOnline[0].address
            let addrs = adds.substring(2, 4) + adds.substring(0, 2);
            let resultt = await homeDeng(token, email, meshUUID, addrs, opCode, values)
            for (let index = 0; index < devisUnOnline.length; index++) {
                let address = devisUnOnline[index].address
                let addr = address.substring(2, 4) + address.substring(0, 2);
                let resultt = await homeDeng(token, email, meshUUID, addr, opCode, values)
                sleep(500)
            }
            SPSet('mqttbufa', JSON.stringify(1));//控制mqtt补发的地方
            meshLog('mqttUtils.js,206行,第一次补发结束');//直接控制OK弹框
        } else {
            //直接结束补发
            // PubSub.publish('mqttguanbi', '')
            SPSet('shuaxincishu', JSON.stringify(1));//控制刷新页面的地方，走不走mqtt获取灯的状态
        }
    } else if (mqttbufa == 1) {
        meshLog('mqttUtils.js,213行,第二次补发');//直接控制OK弹框
        let deviceSetx = JSON.parse(SPGet("deviceSetx"));
        let devisOnline = deviceSetx.filter((item) => {
            if ((item.isOnline == '01') && isLightDevice(item.pid)) {
                return item
            }
        })
        let deviceSet = deviceSetx.filter((item) => {
            if (isLightDevice(item.pid)) {
                return item
            }
        })
        let devisUnOnline = deviceSetx.filter((item) => {
            if ((item.isOnline == '00') && isLightDevice(item.pid)) {
                return item
            }
        })
        meshLog('mqttUtils.js,230行 第二次的deviceSet.length', deviceSet.length);
        meshLog('mqttUtils.js,230行 第二次在线的deviceSet.length', devisOnline.length);
        if (devisUnOnline.length > 1) {
            const opCode = "E3A507";
            const token = SPGet('token')
            const email = SPGet('email')
            const meshUUID = SPGet('meshUUID')
            const values = "1E";
            let adds = devisOnline[0].address
            let addrs = adds.substring(2, 4) + adds.substring(0, 2);
            let resultt = await homeDeng(token, email, meshUUID, addrs, opCode, values)
            for (let index = 0; index < devisUnOnline.length; index++) {
                let address = devisUnOnline[index].address
                let addr = address.substring(2, 4) + address.substring(0, 2);
                let resultt = await homeDeng(token, email, meshUUID, addr, opCode, values)
                sleep(500)
            }
            SPSet('mqttbufa', JSON.stringify(2));//控制mqtt补发的地方
            meshLog('mqttUtils.js,248行,第2次补发结束');//直接控制OK弹框
        } else {
            //PubSub.publish('mqttguanbi', '')
            SPSet('shuaxincishu', JSON.stringify(1));//控制刷新页面的地方，走不走mqtt获取灯的状态
        }
    } else {
        meshLog('mqttUtils.js,254行,直接mqtt数据获取结束,显示弹框,最后的第');//直接控制OK弹框
        let deviceSetx = JSON.parse(SPGet("deviceSetx"));
        let devisOnline = deviceSetx.filter((item) => {
            if ((item.isOnline == '01') && isLightDevice(item.pid)) {
                return item
            }
        })
        let deviceSet = deviceSetx.filter((item) => {
            if (isLightDevice(item.pid)) {
                return item
            }
        })
        meshLog('mqttUtils.js,最后的第的deviceSet.length', deviceSet.length);
        meshLog('mqttUtils.js,最后的第在线的deviceSet.length', devisOnline.length);
        // PubSub.publish('mqttguanbi', '')
        SPSet('shuaxincishu', JSON.stringify(1));//控制刷新页面的地方，走不走mqtt获取灯的状态
    }
    // }, 1000)
    meshLog("mqttUtils.js,271行,wjk mqtt 1", oldTime);
}
//频繁点击判定
function doubleClickJudge() {
    let newTime = new Date().getTime();
    if (newTime - mClicktime < 2000) {
        return;
    }
    mClicktime = new Date().getTime();;
}
function sleep(time) {
    var timeStamp = new Date().getTime();
    var endTime = timeStamp + time;
    while (true) {
        if (new Date().getTime() > endTime) {
            return;
        }
    }
}
